//
//  ProductDetailVC.m
//  Ufan
//
//  Created by yangqingluo on 15/12/6.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "ProductDetailVC.h"
#import "SRRefreshView.h"

@interface ProductDetailVC ()<UIScrollViewDelegate,SRRefreshDelegate>

@property (strong, nonatomic) NSString *htmlString;
@property (nonatomic, strong) UIWebView *showWebView;
@property (nonatomic, strong) SRRefreshView *slimeView;

@end

@implementation ProductDetailVC


- (instancetype)initWithInfo:(NSString *)string{
    self = [super init];
    if (self) {
        self.htmlString = [string copy];
    }
    
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.view addSubview:self.showWebView];
    [self.showWebView.scrollView addSubview:self.slimeView];
    
    [self.slimeView setLoadingWithExpansion];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(245, 245, 245, 1.0);
    [self setupBarButtonItem];
    
    
}

- (void)setupBarButtonItem{
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(12, 0, 12, 48)];
    backButton.tag = 988;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
}

-(void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)refreshInfoFromWeb{
    [self endRefresh];
    [self.showWebView loadHTMLString:[NSString stringWithFormat:@"%@",self.htmlString] baseURL:nil];
}

#pragma getter
- (UIWebView *)showWebView{
    if (!_showWebView) {
        _showWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        _showWebView.scalesPageToFit = YES;
        _showWebView.scrollView.delegate = self;
    }
    
    return _showWebView;
}

- (SRRefreshView *)slimeView{
    if (!_slimeView) {
        _slimeView = [[SRRefreshView alloc] init];
        _slimeView.delegate = self;
        _slimeView.upInset = 0;
        _slimeView.slimeMissWhenGoingBack = YES;
        _slimeView.slime.bodyColor = [UIColor grayColor];
        _slimeView.slime.skinColor = [UIColor grayColor];
        _slimeView.slime.lineWith = 1;
        _slimeView.slime.shadowBlur = 4;
        _slimeView.slime.shadowColor = [UIColor grayColor];
        _slimeView.backgroundColor = [UIColor whiteColor];
    }
    
    return _slimeView;
}

#pragma mark - scrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_slimeView scrollViewDidScroll];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_slimeView scrollViewDidEndDraging];
}



#pragma mark - slimeRefresh delegate
- (void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView{
    [self refreshInfoFromWeb];
}

- (void)endRefresh{
    [_slimeView endRefresh];
}



@end
